<?php
namespace Manage\Controller;
use Think\Controller;

/**
 * Class FixController
 * @package Manage\Controller
 * 维修记录
 */
class AdminFixController extends Controller {

    private $model_name = 'DeviceFix';

    public function index(){
        $this->display('eqm_fix_log');
    }

    public function get_all(){
        $list = get_date_grid($this->model_name);
        echo json_encode($list);
    }

    public function add(){
        $model = D($this->model_name);
        if(IS_POST){
            $data = filterInput($_POST);
            $data['date'] = getNowTime();
            $id = $model->addObj($data, 'id');
            echo json_encode(array('status'=>1, 'message'=>$id));
            return;
        }else if(IS_GET){

        }
        $this->display();
    }

    public function edit(){
        $model = D($this->model_name);
        if(IS_POST){
            $post_data = filterInput($_POST);
            $model->save($post_data);
//            $this->success(LANG('save_success'));
            echo json_encode(array('status'=>1, 'message'=>$post_data['id']));
            return;
        }else if(IS_GET){
            $data = filterInput($_GET);
            $item = $model->queryObj($data['id'], 'id');

            $this->assign('item', $item);
        }
        $this->display();
    }

    public function deleteArray(){
        $data = filterInput($_POST);
        $ids = $data['ids'];
        $model = D($this->model_name);
        $item = $model->deleteArray($ids);
        if($item){
            echo json_encode(array('status'=>1));
        }else{
            echo json_encode(array('status'=>0));
        }
    }

    public function get_obj_json(){
        $id = I('get.id');
        $obj = D($this->model_name)->queryObj($id, 'id');
        echo $obj?json_encode($obj):json_encode(array('status'=> -1));
    }

    public function print_fix_record(){
        $id= I('get.id');
        if(!$id){
            exit;
        }
        $model = D($this->model_name);
        $one = $model->queryObj($id, 'id');
        $this->assign('one', $one);
        $this->display();
    }

    public function export_record(){
        header("Content-type: text/html; charset=utf-8");
        import("Org.Util.PHPExcel");
        import("Org.Util.PHPExcel.Reader.Excel5");
        import("Org.Util.PHPExcel.Writer.Excel5");
        import("Org.Util.PHPExcel.IOFactory.php");
        $pid = I('get.pid')?I('get.pid'):0;
        $start = I('get.start')?I('get.start'):'2016-01-01 00:00:00';
        $end = I('get.end')?I('get.end'):'2016-12-31 00:00:00';
        $str = ' date>=\''.$start.'\' and date<=\''.$end.'\'';
        $record_model = D($this->model_name);
        $temp_file = './excelfile/fixlog.xls';
        $out_file_name = '设备故障记录';
        $php_reader = new \PHPExcel_Reader_Excel5();
        $filename = file_zh_cn_icvon($temp_file);
        if(!$php_reader->canRead($filename)){
            echo '无法识别的Excel文件！';
            return false;
        }
        $php_excel = $php_reader->load($filename);
        $currentSheet = $php_excel->getSheet(0);
        $start_row = 5;
        $row = $start_row;
        //先查找出该泵站的泵机，循环遍历泵机的记录，重复写入
        $currentSheet->setCellValue('A1',$out_file_name);
        $list = $record_model->where($str)->order('date desc')->select();
        foreach($list as $k => $record){
            $currentSheet->setCellValue('B'.$row,$record['name']);
            $currentSheet->setCellValue('C'.$row,$record['errortime']);
            $currentSheet->setCellValue('D'.$row,$record['content']);
            $currentSheet->setCellValue('E'.$row,$record['']);
            $currentSheet->setCellValue('F'.$row,$record['fixtime']);
            $currentSheet->setCellValue('G'.$row,$record['dutyperson']);
            $currentSheet->setCellValue('H'.$row,$record['']);
            $row++;
        }
        $currentSheet->getStyle('A'.$start_row.':H'.$row)->getFont()->setName('宋体');
        $currentSheet->getStyle('A'.$start_row.':H'.$row)->getFont()->setSize(11);
        $currentSheet->getStyle('A'.$start_row.':H'.$row)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $currentSheet->getStyle('A'.$start_row.':H'.$row)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $php_writer = new \PHPExcel_Writer_Excel5($php_excel);
        ob_end_clean();//清除缓冲区,避免乱码
        header('Content-Type: application/vnd.ms-excel');
        $out = substr($start,0,10).'/'.substr($end,0,10).'_'.$out_file_name;
        header("Content-Disposition: attachment;filename=".$out.".xls");
        header('Cache-Control: max-age=0');
        $php_writer->save('php://output');
    }
}